<?php
//include('../lib/log4php/Logger.php');
//Logger::configure('../log/logconfig.xml');

include 'DatabaseDefine.php';
class UserProfileModel {
    
    private $log;
    public function __construct(){
       $this->log = Logger::getLogger(__CLASS__); 
    }
    
    
    private $userId;
    private $genderCode;
    private $titleCode;
    private $firstName;
    private $middleName;
    private $lastName;
    private $address1;
    private $address2;
    private $address3;
    private $street;
    private $subDistrictCode;
    private $districtCode;
    private $provinceCode;
    private $postCode;
    private $telephone;
    private $mobilePhone;
    private $createBy;
    private $createDate;
    private $updateBy;
    private $updateDate;
    
    public function getUserId(){
        return $this->userId;
    }
    
    public function setUserId($userId){
        $this->userId = $userId;
    }
    
    public function getGenderCode(){
        return $this->genderCode;
    }
    
    public function setGenderCode($genderCode){
        $this->genderCode = $genderCode;
    }
    
    public function getTitleCode(){
        return $this->titleCode;
    }
    
    public function setTitleCode($titleCode){
        $this->titleCode = $titleCode;
    }
    
    public function getFirstName(){
        return $this->firstName;
    }
    
    public function setFirstName($firstName){
        $this->firstName = $firstName;
    }

    public function getMiddleName(){
        return $this->middleName;
    }
    
    public function setMiddleName($middleName){
        $this->middleName = $middleName;
    }

    public function getLastName(){
        return $this->lastName;
    }
    
    public function setLastName($lastName){
        $this->lastName = $lastName;
    }
    
    public function getAddress1(){
        return $this->address1;
    }
    
    public function setAddress1($address1){
        $this->address1 = $address1;
    }
    
    public function getAddress2(){
        return $this->address2;
    }
    
    public function setAddress2($address2){
        $this->address2 = $address2;
    }

    public function getAddress3(){
        return $this->address3;
    }
    
    public function setAddress3($address3){
        $this->address3 = $address3;
    }

    public function getStreet(){
        return $this->street;
    }
    
    public function setStreet($street){
        $this->street = $street;
    }
    
    public function getSubDistrictCode(){
        return $this->subDistrictCode;
    }
    
    public function setSubDistrictCode($subDistrictCode){
        $this->subDistrictCode = $subDistrictCode;
    }
    
    public function getDistrictCode(){
        return $this->districtCode;
    }
    
    public function setDistrictCode($districtCode){
        $this->districtCode = $districtCode;
    }
    
    public function getProvinceCode(){
        return $this->provinceCode;
    }
    
    public function setProvinceCode($provinceCode){
        $this->provinceCode = $provinceCode;
    }
    
    public function getPostCode(){
        return $this->postCode;
    }
    
    public function setPostCode($postCode){
        $this->postCode = $postCode;
    }
    
    public function getTelephone(){
        return $this->telephone;
    }
    
    public function setTelephone($telephone){
        $this->telephone = $telephone;
    }
    
    public function getMobilePhone(){
        return $this->mobilePhone;
    }
    
    public function setMobilePhone($mobilePhone){
        $this->mobilePhone = $mobilePhone;
    }
    
    public function getCreateBy(){
        return $this->createBy;
    }
    
    public function setCreateBy($createBy){
        $this->createBy = $createBy;
    }

    public function getCreateDate(){
        return $this->createDate;
    }
    
    public function setCreateDate($createDate){
        $this->createDate = $createDate;
    }

    public function getUpdateBy(){
        return $this->updateBy;
    }
    
    public function setUpdateBy($updateBy){
        $this->updateBy = $updateBy;
    }
    
    public function getUpdateDate(){
        return $this->updateDate;
    }
    
    public function setUpdateDate($updateDate){
        $this->updateDate = $updateDate;
    }
    
    public function toString(){

        return "UserProfileModel To String [".
               "userId=".$this->userId." ".
               "genderCode=".$this->genderCode." ".
               "titleCode=".$this->titleCode." ".
               "firstName=".$this->firstName." ".
               "middleName=".$this->middleName." ".
               "lastName=".$this->lastName." ".
               "address1=".$this->address1." ".
               "address2=".$this->address2." ".
               "address3=".$this->address3." ".
               "street=".$this->street." ".
               "subDistrictCode=".$this->subDistrictCode." ".
               "districtCode=".$this->districtCode." ".
               "provinceCode=".$this->provinceCode." ".
               "postCode=".$this->postCode." ".
               "telephone=".$this->telephone." ".
               "mobilePhone=".$this->mobilePhone." ".
               "createBy=".$this->createBy." ".
               "createDate=".$this->createDate." ".
               "updateBy=".$this->updateBy." ".
               "updateDate=".$this->updateDate." ".
               "roleList=".$this->roleList." ".
               "]";
    }
    
    // Begin Operation User Profile Model
    public function getUserProfileById(){
        try{
            // Begin Open Connection
            include 'DatabaseOpenConnection.php';
            // End Open Connection
            
            $sql = "SELECT * FROM ".TB_USERPROFILE." WHERE UserId ='".$this->userId."' ";
            $this->log->debug($sql);
            $result = mysql_query($sql);
            if (!$result) {
                $errmsg = "Invalid query . ". mysql_error();
                $this->log->error($errmsg);
                throw new Exception($errmsg);
            }
            
            $num_rows = mysql_num_rows($result);
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            
            if($num_rows <= 0){
                $dateTime = date("Y-m-d h:i:s A");
                $refId = rand(10,10000000);
                $response = array();
                $response['loginResponseHeader'] = array( 'refId' => $refId, 'dateTime' => $dateTime,'responseCode'=>'1212','responseDesc'=>'Data Not Found Get User Profile');
                $response['loginResponseDetail'] = array( 'firstname' => '', 'lastname' => '','rolelist'=>'');
                return $response;
            }
            
            
            while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
               $this->userId = $row["UserId"];
               $this->genderCode = $row["GenderCode"];
               $this->titleCode = $row["TitleCode"];
               $this->firstName = $row["FirstName"];
               $this->middleName = $row["MiddleName"];
               $this->lastName = $row["LastName"];
               $this->address1 = $row["Address1"];
               $this->address2 = $row["Address2"];
               $this->address3 = $row["Address3"];
               $this->street = $row["Street"];
               $this->subDistrictCode = $row["SubDistrictCode"];
               $this->districtCode = $row["DistrictCode"];
               $this->provinceCode = $row["ProvinceCode"];
               $this->postCode = $row["PostCode"];
               $this->telephone = $row["Telephone"];
               $this->mobilePhone = $row["MobilePhone"];
               $this->createBy = $row["CreateBy"];
               $this->createDate = $row["CreateDate"];
               $this->updateBy = $row["UpdateBy"];
               $this->updateDate = $row["UpdateDate"];
            }
            
            $dateTime = date("Y-m-d h:i:s A");
            $refId = rand(10,10000000);
            
            $this->log->debug("Before Get RoleList");
            $roleList = $this->getRoleListById($this->userId);
            $this->log->debug("End Get Role List");
            
            //$roleList[] = array( 'role' => 'ROLE1');
            //$roleList[] = array( 'role' => 'ROLE2');
                
            $response = array();
            $response['loginResponseHeader'] = array( 'refId' => $refId, 'dateTime' => $dateTime,'responseCode'=>'0','responseDesc'=>'success');
            $response['loginResponseDetail'] = array( 'firstname' => $this->firstName, 'lastname' => $this->lastName,'rolelist'=>$roleList);
            return $response;
        }catch(Exception $e){
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            throw $e;
        }
    }
    // End Operation User Profile Model
    
    public function getRoleListById($userId){
        try{
            // Begin Open Connection
            include 'DatabaseOpenConnection.php';
            // End Open Connection
            $sql = "SELECT RoleId FROM ".TB_ROLEMAPPING." WHERE UserId ='".$userId."' AND IsActive='1' ";
            $this->log->debug($sql);
            $result = mysql_query($sql);
            if (!$result) {
                $errmsg = "Invalid query . ". mysql_error();
                $this->log->error($errmsg);
                throw new Exception($errmsg);
            }
            
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            
            $roleList = array();
            while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                $roleList[] = array( 'role' => $row["RoleId"]);
            }
            
            return $roleList;
        }catch(Exception $e){
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            throw $e;
        }
    }
    
}

?>
